package com.warrior.superdata.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class DateUtils {

    private final static String CONSTANT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public final static String CONSTANT_DATETIME_FORMAT_DAY = "yyyy-MM-dd";

    public static String addDay(int num, String formatString) {
        formatString = Strings.isBlank(formatString) ? "yyyy-MM-dd HH:mm:ss" : formatString;
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        try {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, -num);
            Date date = c.getTime();
            return format.format(date);
        } catch (Exception e) {
            log.error("时间解析异常");
            return null;
        }
    }

    public static String addDay(int num, int type, String formatString) {
        formatString = Strings.isBlank(formatString) ? "yyyy-MM-dd HH:mm:ss" : formatString;
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        try {
            Calendar c = Calendar.getInstance();
            c.add(type, num);
            Date date = c.getTime();
            return format.format(date);
        } catch (Exception e) {
            log.error("时间解析异常");
            return null;
        }
    }


    /**
     * date类型转换成String类型
     *
     * @param date
     * @param format
     * @return
     */
    public static String convertDateToString(Date date, String format) {
        if (null == date) {
            return null;
        }
        try {
            if (StringUtils.isEmpty(format)) {
                format = CONSTANT_DATETIME_FORMAT;
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(date);
        } catch (Exception e) {
            log.error("======= Date 转换 String异常 msg:{} =======",e.getMessage());
            return null;
        }
    }

    public static Date convertStringToDate(String time, String format){
        try {
            if (StringUtils.isEmpty(format)) {
                format = CONSTANT_DATETIME_FORMAT;
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.parse(time);
        } catch (Exception e) {
            log.error("=======  String 转换 Date异常 msg:{} =======",e.getMessage());
            return null;
        }
    }

    /**
     * @Author xujian
     * @Description String 类型时间转换成LocalDataTime
     * @Date 10:08 2020/9/4
     * @Param [time, format]
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime convertStringToDataTime(String time, String format){
        if (StringUtils.isEmpty(time)) {
            return null;
        }
        try {
            if (StringUtils.isEmpty(format)) {
                format = CONSTANT_DATETIME_FORMAT;
            }

            DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
            return LocalDateTime.parse(time,df);
        } catch (Exception e) {
            log.error("======= String 转换LocalDateTime异常 msg:{} =======",e.getMessage());
            return null;
        }
    }


    public static List<String> getRecentDayAsc(int day) {
        Date now = new Date();
        Calendar instance = Calendar.getInstance();
        List<String> result = new ArrayList<>();
        for (int i = day; i >= 1; i--) {
            instance.setTime(now);
            instance.add(Calendar.DAY_OF_MONTH, -i);
            result.add(convertDateToString(instance.getTime(), CONSTANT_DATETIME_FORMAT_DAY));
        }
        return result;
    }
}
